package androidx.test.runner.permission;

import android.app.UiAutomation;
import android.util.Log;
import androidx.test.InstrumentationRegistry;
import androidx.test.internal.util.Checks;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.codehaus.jackson.util.MinimalPrettyPrinter;

/* loaded from: classes.dex */
class UiAutomationShellCommand extends ShellCommand {
    private static final String TAG = "UiAutomationShellCmd";
    private final PmCommand command;
    private final String permission;
    private final String targetPackage;
    private final UiAutomation uiAutomation = (UiAutomation) Checks.checkNotNull(InstrumentationRegistry.getInstrumentation().getUiAutomation());

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public enum PmCommand {
        GRANT_PERMISSION("grant");
        
        private final String pmCommand;

        PmCommand(String command) {
            String valueOf = String.valueOf(command);
            this.pmCommand = valueOf.length() != 0 ? "pm ".concat(valueOf) : new String("pm ");
        }

        public String get() {
            return this.pmCommand;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public UiAutomationShellCommand(String targetPackage, String permission, PmCommand pmCommand) {
        this.targetPackage = shellEscape(targetPackage);
        this.permission = shellEscape(permission);
        this.command = pmCommand;
    }

    @Override // androidx.test.runner.permission.ShellCommand
    public void execute() throws Exception {
        executePermissionCommand(commandForPermission());
    }

    protected String commandForPermission() {
        return this.command.get() + MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR + this.targetPackage + MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR + this.permission;
    }

    private void executePermissionCommand(String cmd) throws IOException {
        String valueOf = String.valueOf(cmd);
        Log.i(TAG, valueOf.length() != 0 ? "Requesting permission: ".concat(valueOf) : new String("Requesting permission: "));
        try {
            awaitTermination(this.uiAutomation.executeShellCommand(cmd), 2L, TimeUnit.SECONDS);
        } catch (TimeoutException unused) {
            String valueOf2 = String.valueOf(cmd);
            Log.e(TAG, valueOf2.length() != 0 ? "Timeout while executing cmd: ".concat(valueOf2) : new String("Timeout while executing cmd: "));
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:13:0x0038, code lost:
        throw new java.util.concurrent.TimeoutException();
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private static void awaitTermination(android.os.ParcelFileDescriptor r3, long r4, java.util.concurrent.TimeUnit r6) throws java.io.IOException, java.util.concurrent.TimeoutException {
        /*
            long r4 = r6.toMillis(r4)
            r0 = 0
            int r6 = (r4 > r0 ? 1 : (r4 == r0 ? 0 : -1))
            if (r6 <= 0) goto Lf
            long r0 = java.lang.System.currentTimeMillis()
            long r0 = r0 + r4
        Lf:
            r4 = 0
            java.io.BufferedReader r5 = new java.io.BufferedReader     // Catch: java.lang.Throwable -> L40
            java.io.InputStreamReader r6 = new java.io.InputStreamReader     // Catch: java.lang.Throwable -> L40
            android.os.ParcelFileDescriptor$AutoCloseInputStream r2 = new android.os.ParcelFileDescriptor$AutoCloseInputStream     // Catch: java.lang.Throwable -> L40
            r2.<init>(r3)     // Catch: java.lang.Throwable -> L40
            r6.<init>(r2)     // Catch: java.lang.Throwable -> L40
            r5.<init>(r6)     // Catch: java.lang.Throwable -> L40
        L1f:
            java.lang.String r3 = r5.readLine()     // Catch: java.lang.Throwable -> L3d
            if (r3 == 0) goto L39
            java.lang.String r4 = "UiAutomationShellCmd"
            android.util.Log.i(r4, r3)     // Catch: java.lang.Throwable -> L3d
            long r3 = java.lang.System.currentTimeMillis()     // Catch: java.lang.Throwable -> L3d
            int r6 = (r0 > r3 ? 1 : (r0 == r3 ? 0 : -1))
            if (r6 > 0) goto L33
            goto L1f
        L33:
            java.util.concurrent.TimeoutException r3 = new java.util.concurrent.TimeoutException     // Catch: java.lang.Throwable -> L3d
            r3.<init>()     // Catch: java.lang.Throwable -> L3d
            throw r3     // Catch: java.lang.Throwable -> L3d
        L39:
            r5.close()
            return
        L3d:
            r3 = move-exception
            r4 = r5
            goto L41
        L40:
            r3 = move-exception
        L41:
            if (r4 == 0) goto L46
            r4.close()
        L46:
            goto L48
        L47:
            throw r3
        L48:
            goto L47
        */
        throw new UnsupportedOperationException("Method not decompiled: androidx.test.runner.permission.UiAutomationShellCommand.awaitTermination(android.os.ParcelFileDescriptor, long, java.util.concurrent.TimeUnit):void");
    }
}
